/*
 *  encryptutil.c
 *  SecStudy
 *
 *  Created by xudongming on 8/10/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "encryptutil.h"
#include "d3des.h"
#include "rsaeuro.h"
#include <string.h>
#include <stdlib.h>

static char *keystr="PA-BCES-IBP-IBANK-IBP-3DES";
//rsa公匙　若需解密时使用
/*
 static R_RSA_PUBLIC_KEY PA_PUBLIC_KEY1 = {
 1024,
 {0xcb,0x8d,0x46,0x2e,0x8a,0x79,0xaa,0x78,0x21,0xae,0x73,0xbe,0xa4,
 0x7e,0xc9,0x5a,0x92,0xad,0x32,0x71,0xd5,0xa4,0x54,0x83,0xdf,0x69,0x1d,
 0x81,0xff,0x23,0xcd,0xfa,0x66,0xad,0x9d,0xec,0x5f,0x34,0x35,0x14,0xac,
 0x64,0x4b,0x6a,0x10,0xa4,0xd5,0x67,0xf1,0x3c,0x5a,0x2d,0x18,0x25,0xaa,
 0x7d,0x45,0x64,0x26,0x1c,0x9e,0x7b,0x5e,0xa6,0xe4,0x8d,0x8e,0xb7,0xa4,
 0x83,0x61,0xc1,0xff,0x68,0xab,0x67,0x63,0x7d,0x31,0x85,0x3a,0x87,0x31,
 0x7e,0xa3,0x16,0xf9,0x77,0x7e,0x1f,0xe8,0x2d,0x55,0x9a,0xe7,0xbc,0xfa,
 0x00,0x23,0xb1,0x8e,0x04,0xd6,0x26,0xa9,0x58,0xe4,0x35,0x81,0x08,0xf0,
 0x3d,0x9d,0x14,0x29,0x70,0xe2,0xa2,0x4d,0x0e,0x73,0x39,0x4a,0xc6,0xb5,
 0x3c,0x9d,0xad},
 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01}
 };
 */
static R_RSA_PRIVATE_KEY PA_PRIVATE_KEY1 = {
	1024,
	{0xcb,0x8d,0x46,0x2e,0x8a,0x79,0xaa,0x78,0x21,0xae,0x73,0xbe,0xa4,
		0x7e,0xc9,0x5a,0x92,0xad,0x32,0x71,0xd5,0xa4,0x54,0x83,0xdf,0x69,0x1d,
		0x81,0xff,0x23,0xcd,0xfa,0x66,0xad,0x9d,0xec,0x5f,0x34,0x35,0x14,0xac,
		0x64,0x4b,0x6a,0x10,0xa4,0xd5,0x67,0xf1,0x3c,0x5a,0x2d,0x18,0x25,0xaa,
		0x7d,0x45,0x64,0x26,0x1c,0x9e,0x7b,0x5e,0xa6,0xe4,0x8d,0x8e,0xb7,0xa4,
		0x83,0x61,0xc1,0xff,0x68,0xab,0x67,0x63,0x7d,0x31,0x85,0x3a,0x87,0x31,
		0x7e,0xa3,0x16,0xf9,0x77,0x7e,0x1f,0xe8,0x2d,0x55,0x9a,0xe7,0xbc,0xfa,
		0x00,0x23,0xb1,0x8e,0x04,0xd6,0x26,0xa9,0x58,0xe4,0x35,0x81,0x08,0xf0,
		0x3d,0x9d,0x14,0x29,0x70,0xe2,0xa2,0x4d,0x0e,0x73,0x39,0x4a,0xc6,0xb5,
		0x3c,0x9d,0xad},
	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01},
	{0x5f,0x69,0x0d,0xef,0x7c,0xcb,0x00,0xa3,0xa4,0xad,0xff,0xe4,0xe9,0x77,0xe0,
		0x6b,0xca,0xfa,0xd1,0xfd,0x57,0xb5,0xda,0x43,0xa1,0xf1,0xc4,0x7a,0x0d,
		0x9c,0xbb,0xcc,0xd6,0x5a,0x24,0xff,0x09,0xb5,0x0f,0xf8,0x6b,0xc1,0xeb,
		0x33,0x81,0x2f,0xb2,0x34,0x31,0x78,0x27,0x90,0xa1,0x93,0xee,0x31,0x1f,
		0xd8,0x8c,0xa0,0xb7,0x8c,0x7e,0x55,0x88,0x25,0x87,0x29,0x37,0x6e,0x2b,
		0x47,0x05,0xf5,0x92,0x81,0x18,0xb5,0xae,0x9b,0xc8,0xe4,0xda,0xf7,0xb2,
		0xad,0xcd,0x3c,0x05,0xe0,0x0c,0x41,0x5b,0x1f,0x04,0xc3,0x55,0x3e,0x4f,
		0x93,0x37,0xbe,0x40,0xac,0x47,0xfc,0x12,0x06,0xc5,0x25,0x12,0x02,0x64,
		0x6f,0xf4,0x89,0x5c,0xe6,0x29,0xdf,0xd3,0xea,0x77,0x49,0xb7,0xb8,0xfc,0x25},
	{{0xe5,0x05,0x55,0x2f,0x22,0xa4,0x29,0xe9,0xed,0x06,0xb2,0x9e,0x28,0x3a,0x3d,
		0xc7,0xc9,0x0b,0x42,0x4f,0xe0,0x6c,0x69,0x16,0x64,0x2e,0xd3,0x0d,0x4e,0xae,
		0x69,0x73,0x3f,0xfc,0xde,0xcd,0x47,0xd1,0xc2,0x28,0x53,0xc7,0x53,0x4f,0xb2,
		0xc2,0x66,0xf4,0x70,0xfb,0x28,0xd8,0x81,0x0e,0x58,0x05,0x8f,0x43,0xd0,0x26,
		0x41,0x9b,0x07,0x5f},
		{0xe3,0x87,0xdd,0x24,0x04,0x0a,0x25,0xa0,0x0b,0x02,0xc5,0xef,0xea,0x0f,0x8c,
			0x3b,0x95,0xba,0xde,0xef,0x93,0xf5,0xe8,0xa8,0x9c,0x6f,0xe2,0xb4,0x14,0x27,
			0x89,0xb1,0x9a,0xdd,0x5e,0x90,0x29,0x16,0x19,0x43,0xa3,0xad,0x33,0x3c,0xd5,
			0x09,0x10,0xfa,0x61,0xf4,0x71,0x69,0xb5,0x97,0xcd,0x04,0x4f,0x08,0xf1,0xd2,
			0x1e,0xb0,0x72,0x73}},
	{{0xc5,0xee,0x15,0xf0,0xe6,0xbf,0x7a,0x79,0xf1,0xa0,0x18,0x3f,0x18,0x05,0x3d,
		0xdf,0xac,0xa1,0x4e,0x6e,0x3a,0x47,0x77,0x8b,0x22,0x8a,0x55,0x5c,0xea,0xe3,
		0x51,0xbf,0x89,0x4d,0xc2,0x41,0x28,0x10,0xe0,0xfc,0x79,0x6b,0x8b,0x55,0x15,
		0xe9,0x6d,0x91,0x55,0x13,0xbb,0xf9,0x61,0x90,0x44,0x02,0x8a,0x0e,0xd1,0x96,
		0x3e,0x9c,0xfd,0x81},
		{0x08,0x38,0xa0,0x84,0xec,0xa0,0x71,0x3b,0x97,0x2a,0x42,0x7c,0xa8,0xb8,0x67,
			0x18,0xc0,0x10,0x40,0x7d,0xe7,0x9a,0xbb,0x0d,0x5c,0x99,0xae,0x57,0x53,0x63,
			0x1a,0x56,0x90,0x81,0xef,0x28,0xc6,0x53,0x46,0x16,0x6b,0x93,0x96,0x86,0x5c,
			0x2c,0x37,0xb0,0xd4,0x59,0x40,0xbf,0xd1,0x0f,0xeb,0x18,0x7c,0xc2,0x95,0xfa,
			0x41,0xa1,0x9e,0x81}},
	{0x5a,0x73,0x05,0xa4,0x7b,0xb2,0x41,0x5c,0x7a,0x8e,0xe9,0xf7,0xda,0x91,0x73,
		0x92,0x00,0x8c,0x35,0xd9,0x7c,0xf7,0x99,0x7f,0xc1,0x41,0xbe,0x34,0x81,0x9c,
		0x87,0x57,0xbf,0x43,0xa9,0x7d,0x49,0xef,0x64,0x2d,0xeb,0xa2,0xec,0xd9,0xd6,
		0x90,0x14,0xb6,0x7d,0x58,0xba,0x40,0x4f,0x23,0x0f,0x94,0x61,0xd7,0x36,0xe8,
		0x66,0xde,0x16,0x48}
};

/**
 *char*字符串 3des加密方法
 *错误联系 xudongming 
 *email mingstone.xu@gmail.com
 */
unsigned int desedeEncode(char* inbuf,char* outbuf,int inputlen)
{
	return DESede_Encrypt(outbuf,inbuf,inputlen, keystr);
}


/**
 *char*字符串 3des解密方法
 *错误联系 xudongming 
 *email mingstone.xu@gmail.com
 */
unsigned int desedeDecode(char* inbuf,char* outbuf,int inputlen)
{
	int tmpLen=DESede_Decrypt(outbuf,inbuf, inputlen,keystr);
	outbuf[tmpLen]='\0';
	return 0;
}

/**
 *RSA+MD5签名
 *xudongming
 */
unsigned int rsaSign(char* outbuf,unsigned int* outlen,char* inbuf,unsigned int inputlen)
{	
	R_SIGNATURE_CTX context;
	unsigned char signature[MAX_SIGNATURE_LEN];
	unsigned int signatureLen;
	
	int digestAlgorithm = 5;
	unsigned int status=R_SignInit(&context, digestAlgorithm);
	if (status!=0) {
		return status;
	}
	
	status=R_SignUpdate(&context,(unsigned char*)inbuf,inputlen);
	if (status!=0) {
		return status;
	}
	status=R_SignFinal(&context, signature, &signatureLen, &PA_PRIVATE_KEY1);
	if (status!=0) {
		return status;
	}
	
	byteToHex(signature,outbuf, signatureLen);
	*outlen=strlen(outbuf);
	outbuf[*outlen]='\0';
	
	return 0;
}

/**
 *unsigned char* 转换成16进制字符串
 *错误联系 xudongming 
 *email mingstone.xu@gmail.com
 */
int byteToHex(unsigned char* puc,char* pc,int uclength)
{
	for (int i=0; i<uclength; i++) {
		char hex1;
		char hex2;
		
		int value=puc[i];
		int v1=value/16;
		int v2=value%16;
		
		if (v1>=0&&v1<=9) 
		{
			hex1=(char)(48+v1);
		}else {
			hex1=(char)(55+v1);
		}
		
		if (v2>=0&&v2<=9) 
		{
			hex2=(char)(48+v2);
		}else {
			hex2=(char)(55+v2);
		}
		pc[2*i]=hex1;
		pc[2*i+1]=hex2;
		
	}
	return 0;
}

/**
 *16进制字符串 转char*数组
 *错误联系 xudongming 
 *email mingstone.xu@gmail.com
 */
int hexStrTobyte(char* hexStr,char* outbuf)
{
	int len=strlen(hexStr);

	char rs[3];
	memset(rs,0,sizeof(rs));
	
	for (int i=0; i<len/2; i++) 
	{
		rs[0]=hexStr[2*i];
		rs[1]=hexStr[2*i+1];
		char *stop;
		int num=strtol((const char*)&rs, &stop, 16);
		outbuf[i]=(char)num;
	}
	
	return 0;
}